package com.qf.java2201.web;

import com.qf.java2201.dao.OrderDao;
import com.qf.java2201.dao.impl.OrderDaoImpl;
import com.qf.java2201.pojo.HOrder;
import com.qf.java2201.pojo.HUser;
import com.qf.java2201.utils.DateUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.Date;
@WebServlet("/addOrder")
public class AddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String edid = req.getParameter("edid");
        String odate = req.getParameter("odate");
        String sjdid = req.getParameter("sjdid");
        String sickness = req.getParameter("sickness");
        String dmoney = req.getParameter("dmoney");

        HttpSession session = req.getSession();
        HUser u = (HUser) session.getAttribute("u");
        OrderDao orderDao = new OrderDaoImpl();

        HOrder o = new HOrder();
        o.setEdid(Integer.parseInt(edid));
        Date date = DateUtil.strToDate(odate, "yyyy-MM-dd");
        o.setOdate(new Timestamp(date.getTime()));
        o.setSjdid(Integer.parseInt(sjdid));
        o.setUdid(u.getUi().getDid());
        o.setSickness(sickness);
        o.setGhf(Double.valueOf(dmoney));
        o.setYyztid(21);
        int oid = orderDao.addOrder(o);
        o.setOid(oid);
        if(oid > 0) {
            session.setAttribute("oo", o);
            resp.sendRedirect("pay.jsp");
        } else {
            resp.sendRedirect("xq.jsp");
        }
    }
}
